
<?php
class productModel{
	// khai bao cac bien cua model
	public $id;
	public $name;
	public $photo;
	public $sumary;
	public $detail;
	public $post_date;

	public function get_product_by_id($id) {
		$str1 = "SELECT * FROM tbl_product WHERE id=$id";
		$result1 = mysql_query($str1);
		$rows1 = mysql_fetch_array($result1);
		return $rows1;
	}
	public function get_product_by_id2($id) {
		$str2 = "SELECT * FROM tbl_product WHERE id=$id";
		$result2 = mysql_query($str2);
		$rows2 = mysql_fetch_array($result2);
		return $rows2;
	}
	
	public function getForm() // Lay du lieu tu form
	{
		$this->id=@$_POST['id'];
		$this->name=addslashes(@$_POST['name']);
		$this->sumary=@$_POST['sumary'];
		$this->detail=addslashes(@$_POST['detail']);
		$this->post_date=addslashes(@$_POST['post_date']);
		if (is_uploaded_file($_FILES['photo']['tmp_name'])) {
		   $tmp_name = $_FILES['photo']['tmp_name'];
		   $this->photo = "images/data/news/".$_FILES['photo']['name'];
		   move_uploaded_file($tmp_name, "images/data/news/".$_FILES['photo']['name']);
		  }
	}
	
	public function xl_edit()
	{ // sua bo du lieu
    $this->getForm();      
    $sql = "Update tbl_product set 
                    name = '$this->name',                                     
                    photo = '$this->photo',
                    sumary = '$this->sumary',
                    detail = '$this->detail',
                    post_date = '$this->post_date'
            WHERE id = '$this->id'";
    $rs = mysql_query($sql);
    if(!$rs)
        die("Loi truy van: ".$sql);
    header("Location: index.php?mod=product");
	}
	public function xl_insert()
	{
	//them du lieu
	$this->getForm();
	$sql="INSERT INTO tbl_product (name,photo,sumary,detail,post_date) VALUES
					('$this->name',                                     
                    '$this->photo',
                    '$this->sumary',
                    '$this->detail',
                    '$this->post_date')";
    $rs = mysql_query($sql);
    if(!$rs)
        die("Loi truy van: ".$sql);
		ob_clean();
    header("Location: index.php?mod=product");
	}
}
?>